package com.duofan.gmall.product.web;

import com.duofan.gmall.common.constant.AuthServerConstant;
import com.duofan.gmall.common.utils.R;
import com.duofan.gmall.product.entity.CategoryEntity;
import com.duofan.gmall.product.service.CategoryService;
import com.duofan.gmall.product.vo.Catalog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    RedissonClient redisson;
    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping({"/", "index.html"})
    public String indexPage(Model model) {
        // TODO 查处所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        model.addAttribute("categorys", categoryEntities);
        // 视图解析器拼串:
        // classpath:/templates/ + 返回值 + .html
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/json/catalog.json")
    public Map<String,List<Catalog2Vo>> getCatalogJson() {
        Map<String,List<Catalog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        RLock lock = redisson.getLock("my-lock");
//        lock.lock();
        lock.lock(10, TimeUnit.SECONDS);
        try {
            System.out.println("加锁成功,执行业务..." + Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (Exception e) {

        }
        finally {
            lock.unlock();
        }
        return "hello";
    }

    @GetMapping("/write")
    @ResponseBody
    public String writeValue() {
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = UUID.randomUUID().toString();
        RLock writeLock = lock.writeLock();
        try {
            writeLock.lock();
            System.out.println("写锁加锁成功..." + Thread.currentThread().getId());
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writeValue", s);
        } catch (Exception e) {

        } finally {
            writeLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String readValue() {
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        // 加读锁
        RLock readLock = lock.readLock();
        try {
            readLock.lock();
            System.out.println("读锁加锁成功..." + Thread.currentThread().getId());
            Thread.sleep(30000);
            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {

        } finally {
            readLock.unlock();
        }
        return s;
    }

    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "放假了";
    }

    @GetMapping("/go/{id}")
    @ResponseBody
    public String go(@PathVariable("id") Long id) {
        RCountDownLatch door = redisson.getCountDownLatch("door");
        // 计数减一
        door.countDown();
        return id + "班的人走了...";
    }

    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        // 获取一个值
        park.acquire();
        return "ok";
    }

    @GetMapping("/leave")
    @ResponseBody
    public String leave() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        // 释放一个值
        park.release();
        return "ok";
    }
}
